import {createApp} from 'vue'
import './style.css'
import App from './App.vue'
//引入路由router
import router from './router'
import store from './store'
//注册全局组件pannel
import PannelHeader from "./components/pannel.vue"
//注册element-plus图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
//引入element-plus的css 解决弹框不弹出问题
import 'element-plus/dist/index.css'
//解决增加动态路由 然后刷新页面空白的问题 从localstorage中取
const localData = localStorage.getItem('pz_v3pz')
if (localData) {
    store.commit('dynamicMenu', JSON.parse(localData).menu.routerList)
    store.state.menu.routerList.forEach(item => {
        router.addRoute('main', item)
    })
}


//添加路由守卫
router.beforeEach((to, from) => {
        const token = localStorage.getItem('pz_token')
        //非登录页面token不存在
        if (!token && to.path !== '/login') {
            return '/login'
        } else if (token && to.path === '/login') {
            return '/'
        } else {
            return true
        }
    }
)
const app = createApp(App);
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
//注册全局组件pannel
app.component('PannelHeader', PannelHeader)
//路由挂载
app.use(router)
//挂载store
app.use(store)
app.mount('#app')
